package org.vm.cmbcdirect.wechat.utils;

import org.apache.log4j.Logger;
import org.vm.cmbcdirect.wechat.model.component.AccessToken;
import org.vm.cmbcdirect.wechat.service.CommonService;

/**
 * Created by mv on 2016/3/22.
 */
public class TokenThread implements Runnable{
    private final static Logger LOGGER = Logger.getLogger(TokenThread.class);
    public static AccessToken accessToken = null;

    public void run() {
        while (true) {
            try {
                accessToken = CommonService.getAccessToken();
                if (accessToken != null) {
                    LOGGER.info("获取access_token成功，有效时长{" + accessToken.getExpiresIn() + "}秒 token:{" +
                            accessToken.getToken() + "}");
                    // 休眠7000秒
                    Thread.sleep((accessToken.getExpiresIn() - 200) * 1000);
                } else {
                    Thread.sleep(60 * 1000);
                }
            } catch (InterruptedException e) {
                try {
                    Thread.sleep(60 * 1000);
                } catch (InterruptedException e1) {
                    LOGGER.error(e1);
                }
                LOGGER.error(e);
            }
        }
    }
}
